import Vue from 'vue'
import Router from 'vue-router'

import markdown from '@/components/demo/markdown'

import login from '@/components/pages/login'
import top_tree from '@/components/pages/main/top_tree'

import general_index from '@/components/pages/index/general_index'

import user_list from '@/components/pages/system/user_list'
import role_list from '@/components/pages/system/role_list'
import right_list from '@/components/pages/system/right_list'

import log_login from '@/components/pages/log/log_login'
import log_system from '@/components/pages/log/log_system'
import log_newest from '@/components/pages/log/log_newest'

import project_list from '@/components/pages/project/project_info_list'
import project_set from '@/components/pages/project/permission_set_list'

import project_doc_add from '@/components/pages/project/project_doc_add'
import project_doc_edit from '@/components/pages/project/project_doc_edit'
import project_doc_view from '@/components/pages/project/project_doc_view'

import demo from '@/components/demo/parent'

import axios from 'axios';

Vue.use(Router);

//路由配置
const routes = [
  /*{
    path: '/demo/markdown',
    name: 'markdown',
    component: markdown
  },*/
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/main',
    name: 'main',
    component: top_tree,
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },
    children: [
      //系统首页
      {
        path: '/index',
        name: 'index',
        component: general_index,
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      //用户列表
      {
        path: '/system/userList',
        name: 'userList',
        component: user_list,
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      //角色列表
      {
        path: '/system/roleList',
        name: 'roleList',
        component: role_list,
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      //权限列表
      {
        path: '/system/rightList',
        name: 'rightList',
        component: right_list,
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      //登陆日志
      {
        path: '/log/login',
        name: 'logLogin',
        component: log_login,
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      //系统日志
      {
        path: '/log/system',
        name: 'logSystem',
        component: log_system,
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      //日志监控
      {
        path: '/log/newest',
        name: 'logNewest',
        component: log_newest,
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      //项目列表
      {
        path: '/project/list',
        name: 'projectList',
        component: project_list,
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        }
      },
      //文档设置权限
      {
        path: '/project/set',
        name: 'projectSet',
        component: project_set,
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
      }
    ]
  },
  //新增文档内容
  {
    path: '/project/addDoc',
    name: 'projectAddDoc',
    component: project_doc_add,
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  //编辑文档内容
  {
    path: '/project/editDoc',
    name: 'projectEditDoc',
    component: project_doc_edit,
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    }
  },
  //查看文档内容
  {
    path: '/project/viewDoc',
    name: 'projectViewDoc',
    component: project_doc_view,
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    }
  }

];

var router = new Router({
  mode: 'hash',
  routes: routes
});


router.beforeEach((to, from, next) => {
  // 判断该路由是否需要登录权限
  if (to.meta.requireAuth) {
    axios.get("/api/admin/main/checkSession", {}).then((res) => {
      if (res.code != 200) {
        next({
          path: '/login',
          query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
        });
      } else {
        next();
      }
    });
  } else {
    next();
  }
});


export default router
